﻿// <author>Jason Ladley</author>
// <email>jladley@tri-mgroup.com</email>
// <date>09/15/2011</date>
// <summary>Pull up results for select boxes</summary>
// <usage>N/A</usage>
function AjaxLoadDropDown(entityName, methodName, fieldChanged, fieldToUpdate, isWorkflow) {	
	//define a string array that will be passed to the controller's AjaxLoadDropDown method
	//note that field changed is an array object within the array.
	var fieldChangedArrray = fieldChanged.split(",");
	var fieldValueArray = new Array();
	for (var i = 0; i < fieldChangedArrray.length; i++) {
		if ($("#" + fieldChangedArrray[i]).val() == "true") {
			//checkbox must be handled
			//create an array of field values to go allong with the field that changed array.
			if ($("#" + fieldChangedArrray[i]).attr('checked') == "checked") {
				fieldValueArray.push(1);
			}
			else {
				fieldValueArray.push(0);
			}
		}
		else {
			//create an array of field values to go allong with the field that changed array.
			fieldValueArray.push($("#" + fieldChangedArrray[i]).val());
		}
	}
	var JSON_OBJECT_PASSED_TO_CONTROLLER = {
		ENTITY_NAME: entityName,
		METHOD_NAME: methodName,
		FIELD_CHANGED: fieldChanged.split(","),
		FIELD_VALUE: fieldValueArray
	};

	//json for loading data into dropdown.
	$.ajax({
		url: '/AJAX/AjaxLoadDropDown',
		type: "POST",
		async: false,
		data: JSON.stringify(JSON_OBJECT_PASSED_TO_CONTROLLER),
		dataType: "json",
		contentType: "application/json; charset=utf-8",
		success: function (msg) {
			var ddl = $("#" + fieldToUpdate);
			ddl.empty();
			ddl.append('<option></option>');
			//msg is JSON data returned via ajax
			$(msg).each(function () {
				ddl.append(
						$('<option/>', {
							value: this.Value
						}).html(this.Text));
			});
			//if we only have one value select it.
			if (msg.length == 1) {
				ddl.val(msg[0].Value);
				ddl.trigger('change');
			}
			if (isWorkflow == true) {
				//The dropdown may be wrapped in a label so search on layer up as well.
				if ($('#' + fieldToUpdate).siblings(".openDialog").length) {
					$('#' + fieldToUpdate).siblings(".openDialog").show(500); //While loading the drop down find the add link siblings and set their display to true.
				}
				else {
					$('#' + fieldToUpdate).parent().nextAll("a").show(500);
				}
			}
		},
		error: function () {
			//alert("failed.  Please attempt to reload the page.");
			$.meow({
				message: "failed.  Please attempt to reload the page.",
				icon: '../../../images/icons/error.png'
			});
		}
	});
	return true;
}


// <author>Jason Ladley</author>
// <email>jladley@tri-mgroup.com</email>
// <date>11/21/2011</date>
// <summary>return a JSON array to caller</summary>
// <usage>N/A</usage>
function AjaxGetJSONArray(entityName, methodName, valuesPassedIn) {
	//user may pass in multiple values treat the valuesPassedIn parameter as a csv list


	var JSON_OBJECT_PASSED_TO_CONTROLLER = {
		ENTITY_NAME: entityName,
		METHOD_NAME: methodName,
		VALUES_PASSED_IN: valuesPassedIn.split(",")
	};

	//json for loading data into dropdown.
	$.ajax({
		url: '/AJAX/AjaxGetJSONArray',
		type: "POST",
		async: false,
		data: JSON.stringify(JSON_OBJECT_PASSED_TO_CONTROLLER),
		dataType: "json",
		contentType: "application/json; charset=utf-8",
		success: function (msg) {
			ProcessRetrievedJSON(msg);
		},
		error: function () {
			//alert("failed.  Please attempt to reload the page.");
			$.meow({
				message: "failed.  Please attempt to reload the page.",
				icon: '../../../images/icons/error.png'
			});
		}
	});
	return true;
}


// <author>Jason Ladley</author>
// <email>jladley@tri-mgroup.com</email>
// <date>1/30/2011</date>
// <summary>calls a method that sets a value using AJAX</summary>
// <usage>N/A</usage>
function AjaxSetValue(entityName, methodName, valuePassedIn) {
	var JSON_OBJECT_PASSED_TO_CONTROLLER = {
		ENTITY_NAME: entityName,
		METHOD_NAME: methodName,
		VALUES_PASSED_IN: valuePassedIn
	};

	//json for loading data into dropdown.
	$.ajax({
		url: '/AJAX/AjaxSetValue',
		type: "POST",
		async: false,
		data: JSON.stringify(JSON_OBJECT_PASSED_TO_CONTROLLER),
		dataType: "json",
		contentType: "application/json; charset=utf-8",
		success: function (msg) {
		},
		error: function () {
			//alert("failed.  Please attempt to reload the page.");
			$.meow({
				message: "failed.  Please attempt to reload the page.",
				icon: '../../../images/icons/error.png'
			});
		}
	});
	return true;
}